package org.ims.inv.web.action.project;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.ims.inv.model.flow.ModelFlowDef;
import org.ims.inv.model.flow.ModelFlowTemplate;
import org.ims.inv.model.flow.confer.ModelFlowConfer;
import org.ims.inv.model.project.ModelProject;
import org.ims.inv.model.project.ModelProjectConference;
import org.ims.inv.model.project.ModelProjectProcess;
import org.ims.inv.model.system.ModelAppUser;

import cn.trymore.core.util.UtilString;
import cn.trymore.core.web.paging.PaginationSupport;
import cn.trymore.core.web.paging.PagingBean;

/**
 * Action: 项目跟踪
 * 
 * @author Jeccy.Zhao
 *
 */
public class ProjectTrackAction
extends ProjectBaseAction
{
	/**
	 * The LOGGER
	 */
	private static final Logger LOGGER = Logger.getLogger(ProjectTrackAction.class);
	
	/**
	 * <b>[WebAction]</b> 
	 * <br/>
	 * Track Index Page
	 */
	public ActionForward pageProjectTrackIndex (ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
	{
		try
		{
			ModelProject formEntity = (ModelProject) form;
			
			String projectManagerName = request.getParameter("mgr.username");
			if (UtilString.isNotEmpty(projectManagerName))
			{
				ModelAppUser manager = new ModelAppUser();
				manager.setUsername(projectManagerName);
				formEntity.setProjectManager(manager);
			}
			
			// 加载流程环节模板
			ModelFlowDef activatedDef = 
				this.serviceWorkFlow.getServiceFlowDefinition().getCurrentAppliedDefinition();
			if (activatedDef != null)
			{
				request.setAttribute("processes", activatedDef.getTemplates());
			}
			
			request.setAttribute("formEntity", formEntity);
			
			// 获取项目所属行业
			request.setAttribute("dictInds", this.getDictionariesByType("DICT_PRO_INDUSTRY"));
			
			this.outWriteAllParameters(request);
			
			// 待提交材料数量
			request.setAttribute("numProjectForPost", 
					this.getNumProjectForPost());
			
			// 审批中
			request.setAttribute("numProjectForApproving", 
					this.getNumProjectForApproving());
			
			// 退回项目
			request.setAttribute("numProjectForReturn", 
					this.getNumProjectForReturn());
			
			return mapping.findForward("page.project.track.index");
		}
		catch (Exception e)
		{
			LOGGER.error("Exception raised when loading page", e);
			return ajaxPrint(response, getErrorCallback("数据加载失败,原因:" + e.getMessage()));
		}
	}
	
	/**
	 * <b>[WebAction]</b> 
	 * <br/>
	 * Track Tab Switch
	 */
	public ActionForward actionSwtichTab (ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
	{
		try
		{
			String status = request.getParameter("status");
			if (UtilString.isNotEmpty(status))
			{
				ModelProject formEntity = (ModelProject) form;
				formEntity.setProjectStatus(Integer.valueOf(status));
				
				// 项目名称
				if (UtilString.isNotEmpty(formEntity.getProjectName()))
				{
					formEntity.setProjectName(new String(
						formEntity.getProjectName().getBytes("ISO-8859-1"), "UTF-8"));
				}
				
				// 项目负责人
				String projectManagerName = request.getParameter("mgr.username");
				if (UtilString.isNotEmpty(projectManagerName))
				{
					ModelAppUser manager = new ModelAppUser();
					manager.setUsername(
							new String(projectManagerName.getBytes("ISO-8859-1"), "UTF-8"));
					formEntity.setProjectManager(manager);
				}
				
				// 行业类别
				String projectTypeId = request.getParameter("projectTypeId");
				if (UtilString.isNotEmpty(projectTypeId))
				{
					formEntity.setProjectType(this.serviceAppDictionary.get(projectTypeId));
				}
				
				PagingBean pagingBean = this.getPagingBean(request);
				PaginationSupport<ModelProject> pagingData = 
					this.serviceProject.getProjectsByArchiveStatus(formEntity, null, null, pagingBean);
				
				request.setAttribute("datagrid", pagingData);
				request.setAttribute("formEntity", formEntity);
				
				this.outWritePagination(request, pagingBean, pagingData);
				this.outWriteAllParameters(request);
				
				return mapping.findForward("base.project.datagrid");
			}
			else
			{
				return ajaxPrint(response, getErrorCallback("未知项目状态"));
			}
		}
		catch (Exception e)
		{
			LOGGER.error("Exception raised when loading dict dialog page", e);
			return ajaxPrint(response, getErrorCallback("数据加载失败,原因:" + e.getMessage()));
		}
	}
	
	/**
	 * <b>[WebAction]</b> 
	 * <br/>
	 * 根据环节模板ID加载对应的项目列表
	 */
	public ActionForward actionLoadProjectByProcess (ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
	{
		try
		{
			String templateId = request.getParameter("tid");
			if (UtilString.isNotEmpty(templateId))
			{
				ModelFlowTemplate template = 
					this.serviceWorkFlow.getServiceFlowTemplate().get(templateId);
				
				if (template != null)
				{
					ModelProject formEntity = (ModelProject) form;
					
					if (UtilString.isNotEmpty(formEntity.getProjectName()))
					{
						formEntity.setProjectName(new String(
							formEntity.getProjectName().getBytes("ISO-8859-1"), "UTF-8"));
					}
					
					// 项目负责人
					String projectManagerName = request.getParameter("mgr.username");
					if (UtilString.isNotEmpty(projectManagerName))
					{
						ModelAppUser manager = new ModelAppUser();
						manager.setUsername(
								new String(projectManagerName.getBytes("ISO-8859-1"), "UTF-8"));
						formEntity.setProjectManager(manager);
					}
					
					// 行业类别
					String projectTypeId = request.getParameter("projectTypeId");
					if (UtilString.isNotEmpty(projectTypeId))
					{
						formEntity.setProjectType(this.serviceAppDictionary.get(projectTypeId));
					}
					
					ModelProjectProcess currentProcess = new ModelProjectProcess();
					// currentProcess.setNodeTitle(template.getNodeTitle());
					currentProcess.setNodeKey(template.getNodeKey());
					currentProcess.setMaterialStatus(ModelProjectProcess.EProjectMaterialStatus.POSTED.getValue());
					
					formEntity.setCurrentProcess(currentProcess);
					
					PagingBean pagingBean = this.getPagingBean(request);
					PaginationSupport<ModelProject> pagingData = 
						this.serviceProject.getPagination(formEntity, pagingBean);
					
					request.setAttribute("datagrid", pagingData);
					request.setAttribute("formEntity", formEntity);
					
					this.outWritePagination(request, pagingBean, pagingData);
					this.outWriteAllParameters(request);
					
					return mapping.findForward("base.project.datagrid");
				}
				else
				{
					return ajaxPrint(response, getErrorCallback("数据实体不存在."));
				}
			}
			else
			{
				return ajaxPrint(response, getErrorCallback("未知的请求参数."));
			}
		}
		catch (Exception e)
		{
			LOGGER.error("Exception raised when loading page", e);
			return ajaxPrint(response, getErrorCallback("数据加载失败,原因:" + e.getMessage()));
		}
	}
	
	/**
	 * <b>[WebAction]</b> 
	 * <br/>
	 * 根据环节模板ID加载对应的过会预审列表
	 */
	public ActionForward actionLoadProjectConference (ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
	{
		try
		{
			String conferenceId = request.getParameter("cid");
			if (UtilString.isNotEmpty(conferenceId))
			{
				ModelFlowConfer confer = this.serviceWorkFlow.getServiceFlowConfer().get(conferenceId);
				
				if (confer != null)
				{
					ModelProject formEntity = (ModelProject) form;
					
					if (UtilString.isNotEmpty(formEntity.getProjectName()))
					{
						formEntity.setProjectName(new String(
							formEntity.getProjectName().getBytes("ISO-8859-1"), "UTF-8"));
					}
					
					// 项目负责人
					String projectManagerName = request.getParameter("mgr.username");
					if (UtilString.isNotEmpty(projectManagerName))
					{
						ModelAppUser manager = new ModelAppUser();
						manager.setUsername(
								new String(projectManagerName.getBytes("ISO-8859-1"), "UTF-8"));
						formEntity.setProjectManager(manager);
					}
					
					// 行业类别
					String projectTypeId = request.getParameter("projectTypeId");
					if (UtilString.isNotEmpty(projectTypeId))
					{
						formEntity.setProjectType(this.serviceAppDictionary.get(projectTypeId));
					}
					
					PagingBean pagingBean = this.getPagingBean(request);
					PaginationSupport<ModelProjectConference> pagingData = 
						this.serviceProjectConference.getPaginationByEntity(formEntity, confer, null, pagingBean);
					
					request.setAttribute("datagrid", pagingData);
					request.setAttribute("formEntity", formEntity);
					
					this.outWritePagination(request, pagingBean, pagingData);
					this.outWriteAllParameters(request);
					
					return mapping.findForward("base.project.datagrid.conference");
				}
				else
				{
					return ajaxPrint(response, getErrorCallback("数据实体不存在."));
				}
			}
			else
			{
				return ajaxPrint(response, getErrorCallback("未知的请求参数."));
			}
		}
		catch (Exception e)
		{
			LOGGER.error("Exception raised when loading page", e);
			return ajaxPrint(response, getErrorCallback("数据加载失败,原因:" + e.getMessage()));
		}
	}
	
	public static Logger getLogger()
	{
		return LOGGER;
	}
	
}
